package cn.ffcs.uec.outStaff.component;

import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.util.media.Media;
import org.zkoss.web.servlet.Servlets;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffEncListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.staff.manager.UecEncManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecEnc;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.FileUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:奖励信息 CRUD插件
 * @Description:
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @Version 0.0.1 date:2017年11月27日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecOutStaffEncListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	@Getter
	private UecOutStaffEncListboxBean bean = new UecOutStaffEncListboxBean();

	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");
	/**
	 * 人员奖励信息
	 */
	private UecEnc uecEnc;
	/**
	 * 翻页查询条件实体
	 */
	private UecEnc encPaging;

	private UecEnc approvalEncPaging;

	/**
	 * 上传文件
	 */
	private Media media;

	/**
	 * 存放上传数据
	 */
	private List<UecEnc> uecEncs = new ArrayList<UecEnc>();

	private static final int TOTAL_COLUMN = 8;

	private UecEncManager uecEncManager = (UecEncManager) ApplicationContextUtil
			.getBean("uecEncManager");

	private List<UecEnc> encList = new ArrayList<UecEnc>();

	private boolean isOutStaffEntryExt = false;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffEncListboxExt() {
		Executions.createComponents(
				UecOutStaffConstants.UEC_OUT_STAFF_ENC_EXT_LISTBOX_ZUL, this,
				null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY, this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY_RESPONSE);

		this.addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY_OF_OUTSTAFF_ENTRY,
				this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY_OF_OUTSTAFF_ENTRYRESPONSE);

		this.addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY_OF_OUTSTAFF_ENTRY_VIEW,
				this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY_OF_OUTSTAFF_VIEW_ENTRY_RESPONSE);

		this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_ENC, this,
				UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_ENC_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false, false, false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecOutStaffEncList() throws Exception {
		if (this.bean.getUecOutStaffEncListbox().getSelectedCount() > 0) {
			uecEnc = (UecEnc) bean.getUecOutStaffEncListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * ，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffEncQueryResponse(ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (null != uecOutStaffInfo) {
	        //设置操作按钮，非盟市主管不可操作按钮
	        if(PermissionUtil.isNoMainOutStaff()) {
	            this.setButtonValid(false, false, true, true,true,true);
	        }else {
	            this.setButtonValid(true, true, true, true, true, true);
	        }
			uecEnc = new UecEnc();
			uecEnc.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecEnc.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			encPaging = uecEnc;
			this.onQueryUecOutStaffEnc();
		}
	}

	/**
	 * 抛出事件， 事件监听 ,来自入职页
	 * 
	 * @param event
	 */
	@SuppressWarnings("unchecked")
	public void onUecOutStaffEncQueryOfOutStaffEntryResponse(ForwardEvent event) {
		uecEnc = null;
		Map<String, Object> map = (Map<String, Object>) event.getOrigin()
				.getData();
		isOutStaffEntryExt = (Boolean) map.get("isOutStaffEntryExt");
		uecOutStaffInfo = (UecOutStaffInfo) map.get("uecOutStaffInfo");
		if (null != uecOutStaffInfo) {
			this.setButtonValid(true, true, true, true, true, false);
			// 显示所有列表
			ListModel list = new BindingListModelList(encList, true);
			this.bean.getUecOutStaffEncListbox().setModel(list);
			// 取消翻页
			this.bean.getUecOutStaffEncListboxPaging().setTotalSize(1);
		}
	}

	/**
	 * 奖励审批信息id，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffEncQueryOfOutStaffEntryViewResponse(
			ForwardEvent event) {
		encList.clear();
		approvalEncPaging = new UecEnc();
		String encIds = (String) event.getOrigin().getData();
		if (!StrUtil.isNullOrEmpty(encIds)) {
			String[] encIdArray = encIds.split("-");
			if (!StrUtil.isNullOrEmpty(encIdArray) && encIdArray.length > 0) {
				for (String encId : encIdArray) {
					UecEnc uecEnc = UecEnc.newInstance();
					uecEnc.setEncId(Long.valueOf(encId));
					uecEnc = uecEncManager.findUecEncById(uecEnc);
					encList.add(uecEnc);
				}

				onQueryUecOutStaffEnc();
			}
		}

	}

	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecOutStaffEncResponse(ForwardEvent event) {
		clearVariable();
		uecOutStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecOutStaffEncListbox());
		this.bean.getUecOutStaffEncListboxPaging().setTotalSize(1);

		this.setButtonValid(false, false, false, false, false, false);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canDownload, boolean canUpload,
			boolean canView, boolean canAdd, boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);

		this.bean.getDownloadButton().setDisabled(!canDownload);
		this.bean.getUploadButton().setDisabled(!canUpload);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecOutStaffEnc() {
		this.bean.getUecOutStaffEncListboxPaging().setActivePage(0);
		this.onQueryUecOutStaffEncList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecOutStaffEnc() {
		ListboxUtils.clearListbox(this.bean.getUecOutStaffEncListbox());
		this.bean.getUecOutStaffEncListboxPaging().setTotalSize(1);
	}

	/**
	 * 下载模版
	 */
	@SuppressWarnings("deprecation")
	public void onDownloadTemplate() {
		try {
			String charset = "UTF-8";
			// 服务器文件名
			String fileName = "uecOutStaffEnc_template.xls";
			// 编码后文件名
			String encodedName = null;
			encodedName = URLEncoder.encode(fileName, charset);
			// 将空格替换为+号
			encodedName = encodedName.replace("%20", "+");
			HttpServletRequest httpRequest = (HttpServletRequest) Executions
					.getCurrent().getNativeRequest();
			// 解决ie6 bug 或者是火狐浏览器
			if (encodedName.length() > 150 || Servlets.isGecko(httpRequest)
					|| Servlets.isGecko3(httpRequest)) {
				encodedName = new String(fileName.getBytes(charset),
						"ISO8859-1");
			}
			Filedownload.save(
					new FileInputStream(httpRequest
							.getRealPath("/pages_nmg/uec_outStaff/doc/"
									+ fileName)), "application/octet-stream",
					encodedName);
		} catch (Exception e) {
			ZkUtil.showError("下载员工奖励信息导入模版失败。", "系统提示");
		}
	}

	/**
	 * 文件上传 导入员工
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onUpload$uploadButton(final ForwardEvent forwardEvent)
			throws Exception {

		// 清除变量
		uecEncs.clear();

		if (!isOutStaffEntryExt) {
			UecTrial qryUecTrial = UecTrial.newInstance();
			qryUecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
			qryUecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
			if (null == qryUecTrial) {
				ZkUtil.showError("此人未入职,请先执行入职操作", "温馨提示");
				return;
			}
		}

		String tips = "";
		if (!ApprovalUtil.getActivityFlag()) {
			tips = "确认批量导入文件？导入后将直接保存到人员信息！";
		} else {
			tips = "确认批量导入文件？导入后将直接发起流程！";
		}
		Messagebox.show(tips, "输入确认", Messagebox.OK | Messagebox.CANCEL,
				Messagebox.QUESTION, new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (((Integer) event.getData()).intValue() == Messagebox.OK) {
							bean.getUecOutStaffEncListbox().clearSelection();

							media = ((UploadEvent) forwardEvent.getOrigin())
									.getMedia();
							if (null == media) {
								ZkUtil.showError("请选择要上传的文件!", "系统提示");
								return;
							}
							String fileName = media.getName();
							if (fileName.endsWith(".xls")
									|| fileName.endsWith(".xlsx")) {
								// readsUpLoadFile();
							} else {
								ZkUtil.showError(
										"导入的文件必须是以.xls或.xlsx结尾的EXCEL文件!",
										"系统提示");
								return;
							}
							String[][] objArray = FileUtil.readExcel(media, 1,
									1);
							if (objArray == null || objArray.length == 0) {
								ZkUtil.showError("导入文件没有数据！", "错误信息");
								return;
							}
							setDataToRows(objArray, fileName);
						} else {
							return;
						}
					}
				});

	}

	private void setDataToRows(String[][] data, String fileName) {

		String[] row = null;
		boolean isUpload = true;
		for (int i = 0; i < data.length; i++) {
			row = data[i];
			if (row.length != TOTAL_COLUMN + 1) {
				ZkUtil.showError("导入文件数据不正确！", "错误信息");
				return;
			}

			UecEnc uecEnc = UecEnc.newInstance();
			for (int j = 0; j < TOTAL_COLUMN; j++) {

				switch (j) {
				// 奖励事由
				case 0:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("奖励事由未填写", "错误提示");
						isUpload = false;
						return;
					}
					uecEnc.setEncourmatter(data[i][j]);
					break;
				// 奖励日期
				case 1:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("奖励日期未填写", "错误提示");
						isUpload = false;
						return;
					}
					try {
						new SimpleDateFormat("yyyy-MM-dd").parse(data[i][j]);
					} catch (ParseException e) {
						e.printStackTrace();
						ZkUtil.showError("奖励日期格式不正确！", "错误提示");
						isUpload = false;
						return;
					}
					uecEnc.setEncourdate(data[i][j]);
					break;
				// 奖励类型
				case 2:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("奖励类型未填写", "错误提示");
						isUpload = false;
						return;
					}
					uecEnc.setEncourtype(data[i][j]);
					break;
				// 奖励措施
				case 3:
					uecEnc.setEncourmeas(data[i][j]);
					break;
				// 奖励名称
				case 4:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("奖励名称为空！", "错误提示");
						isUpload = false;
						return;
					}
					uecEnc.setGlbdef1(data[i][j]);
					break;
				// 奖励档次
				case 5:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("奖励档次为空！", "错误提示");
						isUpload = false;
						return;
					}
					String glbdef5 = null;
					if ("一档".equals(data[i][j])) {
						glbdef5 = "1001A1100000000005YJ";
					} else if ("二档".equals(data[i][j])) {
						glbdef5 = "1001A1100000000005YK";
					} else if ("三档".equals(data[i][j])) {
						glbdef5 = "1001A1100000000005YL";
					} else if ("四档".equals(data[i][j])) {
						glbdef5 = "1001A1100000000005YM";
					} else if ("五档".equals(data[i][j])) {
						glbdef5 = "1001A1100000000005YN";
					} else {
						ZkUtil.showError("奖励档次填写不正确", "错误提示");
						return;
					}
					uecEnc.setGlbdef5(glbdef5);
					break;
				// 奖励机构
				case 6:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("奖励机构为空！", "错误提示");
						isUpload = false;
						return;
					}
					uecEnc.setEncourorg(data[i][j]);
					break;
				// 奖励级别
				case 7:
					if (StrUtil.isNullOrEmpty(data[i][j])) {
						ZkUtil.showError("奖励级别为空！", "错误提示");
						isUpload = false;
						return;
					}
					String encLevel = null;
					if ("省部级".equals(data[i][j])) {
						encLevel = "1001A1100000000005YS";
					} else if ("省公司级".equals(data[i][j])) {
						encLevel = "1001A1100000000005YU";
					} else if ("市公司级".equals(data[i][j])) {
						encLevel = "1001A1100000000005YV";
					} else if ("国家级".equals(data[i][j])) {
						encLevel = "1001A1100000000005YR";
					} else {
						ZkUtil.showError("奖励级别填写不正确", "错误提示");
						return;
					}
					uecEnc.setEncourrank(encLevel);
					break;
				default:
					break;
				}

			}
			if (!isUpload) {
				return;
			}

			uecEnc.setStaffId(uecOutStaffInfo.getOutStaffId());
			// 外包标志，很重要
			uecEnc.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			// 创建时间
			uecEnc.setCreationtime(DateUtil.getCurrenDate());
			// 创建人
			uecEnc.setCreator(PlatformUtil.getCurrentUser().getScreenName());
			uecEnc.setPkOrg(uecOutStaffInfo.getPkHrorg());
			uecEnc.setDr(0L);
			uecEnc.setLastflag("Y");
			uecEnc.setRecordnum(0L);
			uecEncs.add(uecEnc);
		}

		if (null != uecEncs) {
			encList.addAll(uecEncs);
			ListModel list = new BindingListModelList(encList, true);
			this.bean.getUecOutStaffEncListbox().setModel(list);
			this.bean.getUecOutStaffEncListboxPaging().setTotalSize(1);

			if (isOutStaffEntryExt) {
				postEncList();
			} else {
				if (ApprovalUtil.getActivityFlag()) {
					if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
						ZkUtil.showError("当前登录用户已失效，请重新登录!", "错误提示");
						return;
					}
					String formTheme = "盟市外包人员奖励批量申请流程";
					String currentName = PlatformUtil.getCurrentUser()
							.getScreenName();
					String procInstId = null;
					for (UecEnc uEnc : uecEncs) {
						Long outStaffId = uecOutStaffInfo.getOutStaffId();

						uEnc.addStatusCdIS3();
						Long encId = uEnc.getEncId();

						String startFlowJson = "{" + "formTheme: '" + formTheme
								+ "'," + " formContent: '奖励人员:"
								+ uecOutStaffInfo.getPsnName() + "',"
								+ " procDefId: '"
								+ ApprovalConstants.OUT_STAFF_ENC_PROC_DEF_ID
								+ "'," + " urgent: '1',"
								+ " atiActCategoryId: '"
								+ ApprovalConstants.MODIFICATION_CATEGORY_ID
								+ "'," + " procDefKey: '"
								+ ApprovalUtil.OUT_STAFF_ENC_EDIT + "',"
								+ " formSender: '" + currentName + "',"
								+ " primaryId: '{" + "uec_out_staff_info_his:"
								+ outStaffId + ",uec_out_staff_info:"
								+ outStaffId + ",uec_enc:" + encId + "'}";

						try {
							procInstId = ApprovalUtil
									.startApproval(startFlowJson);
						} catch (Exception e) {
							// 流程发起失败 数据改为失效
							uEnc.removeOnly();
							onResetUecOutStaffEnc();
							ZkUtil.showError("流程发起失败！", "错误提示");
							e.printStackTrace();
						}

						if (StrUtil.isNullOrEmpty(procInstId)) {
							uEnc.removeOnly();
							onResetUecOutStaffEnc();
							ZkUtil.showError("流程发起失败！", "错误提示");
							return;
						}
						// 第一审批人如果是发起人直接到人力审批环节
						ApprovalUtil.isCurrentAssignNames(procInstId);
					}
					ApprovalUtil.showNextAssignee(procInstId, formTheme);
				} else {
					for (UecEnc uEnc : uecEncs) {
						uEnc.addOnly();
					}
					ZkUtil.showInformation("批量导入奖励信息已保存成功!", "温馨提示");
				}
			}

		}

	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecOutStaffEnc() throws Exception, InterruptedException {
		if (null != uecEnc) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecEnc", uecEnc);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_ENC_EDIT_ZUL, this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecOutStaffEnc() throws Exception {
		if (uecOutStaffInfo != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_ENC_EDIT_ZUL, this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecEnc = (UecEnc) event.getData();
						encList.add(uecEnc);
						if (!isOutStaffEntryExt) {
							onQueryUecOutStaffEncList();
						} else {
							ListModel list = new BindingListModelList(encList,
									true);
							bean.getUecOutStaffEncListbox().setModel(list);
							bean.getUecOutStaffEncListboxPaging().setTotalSize(
									1);

							postEncList();
						}

					}
				}
			});
		}
	}

	/**
	 * 抛出事件，把添加的列表发送给OutStaffEntryExt
	 */
	private void postEncList() {
		Events.postEvent("onPostEncListOfOutStaffEntry", this, encList);
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecOutStaffEnc() throws Exception, InterruptedException {
		if (uecEnc != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecEnc", uecEnc);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_ENC_EDIT_ZUL, this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						if (isOutStaffEntryExt) {
							encList.remove(uecEnc);
						}
						uecEnc = (UecEnc) event.getData();
						// 新增或修改完成后，列表从数据库更新
						if (!isOutStaffEntryExt) {
							onQueryUecOutStaffEncList();
						} else {
							encList.add(uecEnc);
							ListModel list = new BindingListModelList(encList,
									true);
							bean.getUecOutStaffEncListbox().setModel(list);
							bean.getUecOutStaffEncListboxPaging().setTotalSize(
									1);

							postEncList();
						}

					}
				}
			});
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}

	/**
	 * 审批状态中修改按钮
	 * 
	 * @throws InterruptedException
	 * @throws SuspendNotAllowedException
	 */
	public void onApprovalEditUecOutStaffEnc()
			throws SuspendNotAllowedException, InterruptedException {
		if (uecEnc != null && uecEnc.getId() != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "approvalEdit");
			map.put("uecEnc", uecEnc);
			map.put("isOutStaffEntryExt", isOutStaffEntryExt);
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_ENC_EDIT_ZUL, this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						uecEnc = (UecEnc) event.getData();
						// 新增或修改完成后，列表从数据库更新
						PageInfo pageInfo = uecEncManager
								.queryPageInfoByUecEncOnApproval(uecEnc, bean
										.getUecOutStaffEncListboxPaging()
										.getActivePage() + 1, bean
										.getUecOutStaffEncListboxPaging()
										.getPageSize());
						ListModel list = new BindingListModelList(pageInfo
								.getDataList(), true);
						bean.getUecOutStaffEncListbox().setModel(list);
						bean.getUecOutStaffEncListboxPaging().setTotalSize(1);
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要修改的记录", "提示信息");
			return;
		}

	}

	/**
	 * 删除按钮
	 */
	public void onDelUecOutStaffEnc() {
		if (uecEnc.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecEnc.removeOnly();
						PubUtil.reDisplayListbox(
								bean.getUecOutStaffEncListbox(), uecEnc, "del");
						uecEnc = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页--onUecOutStaffEncListboxPaging
	 */
	public void onUecOutStaffEncListboxPaging() {
		this.onQueryUecOutStaffEncList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecEnc = null;
		encPaging = null;
		encList.clear();
	}

	/**
	 * 查询
	 */
	public void onQueryUecOutStaffEncList() {
		// 翻页查看生效数据
		if (null != encPaging) {
			PageInfo pageInfo = uecEncManager
					.queryPageInfoByUecEnc(encPaging,
							this.bean.getUecOutStaffEncListboxPaging()
									.getActivePage() + 1, this.bean
									.getUecOutStaffEncListboxPaging()
									.getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecOutStaffEncListbox().setModel(list);
			this.bean.getUecOutStaffEncListboxPaging().setTotalSize(
					pageInfo.getTotalCount());

		}
		// 翻页查看 审批数据
		if (null != approvalEncPaging) {
			// List<UecEnc> pageEncs = getCurrentPageList(encList, bean
			// .getUecOutStaffEncListboxPaging().getActivePage() + 1, bean
			// .getUecOutStaffEncListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(encList, true);
			this.bean.getUecOutStaffEncListbox().setModel(list);
			this.bean.getUecOutStaffEncListboxPaging().setTotalSize(1);
		}
		uecEnc = null;
	}

	/**
	 * 根据list 获取当前list
	 * 
	 * @param list
	 * @param activePage
	 * @param pageSize
	 * @return
	 */
	public List<UecEnc> getCurrentPageList(List<UecEnc> list, int activePage,
			int pageSize) {
		int fromIndex = activePage * pageSize;
		int toIndex = fromIndex + pageSize;
		if (fromIndex >= list.size()) {
			return null;
		}
		if (toIndex >= list.size()) {
			return list.subList(fromIndex, list.size());
		}
		return list.subList(fromIndex, toIndex);

	}

}
